package com.zhuhjay.eduservice.controller.front;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhuhjay.commonutils.JwtUtils;
import com.zhuhjay.commonutils.R;
import com.zhuhjay.eduservice.client.OrderClient;
import com.zhuhjay.eduservice.entity.EduChapter;
import com.zhuhjay.eduservice.entity.EduCourse;
import com.zhuhjay.eduservice.entity.EduTeacher;
import com.zhuhjay.eduservice.entity.EduVideo;
import com.zhuhjay.eduservice.entity.frontvo.CourseFrontVo;
import com.zhuhjay.eduservice.entity.frontvo.CourseWebVo;
import com.zhuhjay.eduservice.service.EduChapterService;
import com.zhuhjay.eduservice.service.EduCourseService;
import com.zhuhjay.eduservice.service.EduVideoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author ZhuHJay
 * @date 2022/9/8 20:30
 */
@Api(description = "前端课程控制器")
@RestController
@RequestMapping("/eduservice/coursefront")

public class CourseFrontController {

    @Resource
    private EduCourseService courseService;

    @Resource
    private EduChapterService chapterService;

    @Resource
    private OrderClient orderClient;

    @ApiOperation(value = "前端分页展示课程列表")
    @PostMapping("getFrontCourseList/{current}/{limit}")
    @SuppressWarnings("all")
    public R getFrontCourseList(@PathVariable Long current, @PathVariable Long limit,
                                @RequestBody CourseFrontVo search) {
        Page<EduCourse> coursePage = new Page<>(current, limit);
        courseService.page(coursePage, new LambdaQueryWrapper<EduCourse>()
                .eq(StringUtils.hasLength(search.getTitle()), EduCourse::getTitle, search.getTitle())
                .eq(StringUtils.hasLength(search.getTeacherId()), EduCourse::getTeacherId, search.getTeacherId())
                .eq(StringUtils.hasLength(search.getSubjectParentId()), EduCourse::getSubjectParentId, search.getSubjectParentId())
                .eq(StringUtils.hasLength(search.getSubjectId()), EduCourse::getSubjectId, search.getSubjectId())
                .orderByDesc(StringUtils.hasLength(search.getBuyCountSort()), EduCourse::getBuyCount)
                .orderByDesc(StringUtils.hasLength(search.getGmtCreateSort()), EduCourse::getGmtCreate)
                .orderByDesc(StringUtils.hasLength(search.getPriceSort()), EduCourse::getPrice)
        );
        return R.ok()
                .data("items", coursePage.getRecords())
                .data("current", coursePage.getCurrent())
                .data("pages", coursePage.getPages())
                .data("size", coursePage.getSize())
                .data("total", coursePage.getTotal())
                .data("hasNext", coursePage.hasNext())
                .data("hasPrevious", coursePage.hasPrevious());
    }

    @ApiOperation(value = "前端获取课程详细信息")
    @GetMapping("getFrontCourseInfo/{id}")
    public R getFrontCourseInfo(@PathVariable String id, HttpServletRequest request) {
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        boolean buyCourse = false;
        if (StringUtils.hasLength(memberId)) {
            buyCourse = orderClient.isBuyCourse(id, memberId);
        }
        return R.ok().data("courseWebVo", courseService.getFrontCourseInfo(id))
                .data("chapterVideoList", chapterService.getChapterVideoByCourseId(id))
                .data("isBuy", buyCourse);
    }

}
